package com.lengxf.job.test;

import com.lengxf.job.dynamicTask.DynamicTaskService;
import com.lengxf.job.dynamicTask.Task;
import com.lengxf.job.dynamicTask.finder.TaskTypeEnum;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@AllArgsConstructor
public class TaskTestController {

    private final DynamicTaskService dynamicTaskService;

    private final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @PostMapping("addTask")
    public boolean addTask(@RequestParam("time") String time) {
        LocalDateTime parse = LocalDateTime.parse(time, formatter);
        Task task = new Task();
        task.setType(TaskTypeEnum.LOG_TASK);
        task.setStartTime(parse);
        return dynamicTaskService.add(task);
    }

    @PostMapping("stopTask")
    public boolean stopTask(@RequestParam("time") String time) {
        LocalDateTime parse = LocalDateTime.parse(time, formatter);
        Task task = new Task();
        task.setType(TaskTypeEnum.LOG_TASK);
        task.setStartTime(parse);
        dynamicTaskService.stop(task);
        return true;
    }

    @PostMapping("queryList")
    public Object queryList() {
        return dynamicTaskService.getTaskList();
    }

}
